package com.ztgf.shopping.api.client.template;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.OrderCommodityHighBO;
import com.ztgf.commodity.models.service.ICommodityInfoService;
import com.ztgf.commodity.models.vo.CommodityPriceVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.order.models.service.IOrderCommodityRelationService;
import com.ztgf.shopping.api.common.SysLogs;
import com.ztgf.shopping.models.bo.template.TempletInfoBO;
import com.ztgf.shopping.models.bo.template.TempletModuleBO;
import com.ztgf.shopping.models.dto.template.TempletInfoDTO;
import com.ztgf.shopping.models.dto.template.TempletModuleDTO;
import com.ztgf.shopping.models.dto.template.TempletPlatformRelationDTO;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.service.template.ITempletInfoService;
import com.ztgf.shopping.models.service.template.ITempletModuleService;
import com.ztgf.shopping.models.service.template.ITempletPlatformRelationService;
import com.ztgf.shopping.models.vo.template.TemplateModuleSortVO;
import com.ztgf.shopping.models.vo.template.TempletInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 模版  前端控制器
 * </p>
 *
 * @author YUE
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/api/templetInfo")
@Slf4j
@Api(value = "TempletInfoApiController", tags = "模版表Api接口")
public class TempletInfoApiController {
    @DubboReference
    private ITempletInfoService iTempletInfoService;

    @DubboReference
    private IOrderCommodityRelationService orderCommodityRelationService;

    @DubboReference
    private ICommodityInfoService commodityInfoService;

    @DubboReference
    private IShopInfoService shopInfoService;

    @DubboReference
    private ITempletModuleService templetModuleService;

    @DubboReference
    private ITempletPlatformRelationService templetPlatformRelationService;

//    /**
//     * 创建默认的店铺信息
//     * @return
//     */
//    @ApiOperation(value = "创建默认的店铺信息",notes = "创建默认的店铺信息")
//    @PostMapping(value = "/createDefaultTemplateByShopId")
//    public Result createDefaultTemplateByShopId(Long shopId){
//        iTempletInfoService.createDefaultTemplateByShopId(shopId);
//        return new Result();
//    }

    /**
     * 获取平台首页 所有功能
     *
     * @return
     */
    @ApiOperation(value = "平台首页 所有模块", notes = "平台首页 所有模块")
    @PostMapping(value = "/getListPlatformTemplateModule")
    public Result<List<TempletModuleDTO>> getListPlatformTemplateModule() {
        List<TempletModuleDTO> templetModuleDTOList = iTempletInfoService.getListPlatformTemInfoModule();
//        TemplateModuleSortVO templateModuleSortVO = new TemplateModuleSortVO();
//        List<Integer> sortList = new ArrayList<>();
//        //排序
//        for (TempletModuleDTO templetModuleDTO : templetModuleDTOList) {
//            if (templetModuleDTO.getKey().equals(ShoppingCodeEnum.DECORATE_BANNER.getMsg())) {//banner
//                sortList.add(ShoppingCodeEnum.DECORATE_BANNER.getCode());
//                continue;
//            }
//            if (templetModuleDTO.getKey().equals(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getMsg())) {//主导航
//                sortList.add(ShoppingCodeEnum.DECORATE_MAIN_NAVIGATION.getCode());
//                continue;
//            }
//            if (templetModuleDTO.getKey().equals(ShoppingCodeEnum.DECORATE_SECTION.getMsg())) {//频道
//                sortList.add(ShoppingCodeEnum.DECORATE_SECTION.getCode());
//                continue;
//            }
//            if (templetModuleDTO.getKey().equals(ShoppingCodeEnum.DECORATE_TOPIC.getMsg())) {//推荐专区
//                sortList.add(ShoppingCodeEnum.DECORATE_TOPIC.getCode());
//                continue;
//            }
//            if (templetModuleDTO.getKey().equals(ShoppingCodeEnum.DECORATE_SHOW_WINDOW.getMsg())) {//新鲜好物
//                sortList.add(ShoppingCodeEnum.DECORATE_SHOW_WINDOW.getCode());
//                continue;
//            }
//            if (templetModuleDTO.getKey().equals(ShoppingCodeEnum.DECORATE_ZTGF_INFO.getMsg())) {//中铁信息
//                sortList.add(ShoppingCodeEnum.DECORATE_ZTGF_INFO.getCode());
//            }
//        }
//        templateModuleSortVO.setTempletModuleDTOList(templetModuleDTOList);//添加功能
//        templateModuleSortVO.setSortList(sortList);//添加排序
        return new Result<>(templetModuleDTOList);
    }


    /**
     * 获取平台首页 所有功能
     *
     * @return
     */
    @ApiOperation(value = "平台首页 所有模块", notes = "平台首页 所有模块")
    @PostMapping(value = "/queryHomeTempletModuleList")
    public Result<List<TempletModuleDTO>> queryHomeTempletModuleList() {
        Wrapper<TempletInfoDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", 0);
        TempletInfoDTO tempDto = iTempletInfoService.selectOne(wrapper);
        if (tempDto == null) {
            return new Result<>(CodeEnum.FAIL_4);
        }
        Long tempId = tempDto.getId();
        Wrapper<TempletModuleDTO> wrapperA = new EntityWrapper<>();
        wrapperA.eq("templet_id", tempId);
        wrapperA.eq("is_publish", 1);
        wrapperA.eq("status", 0);
//        wrapperA.orderBy("sort", true);
        wrapperA.orderBy("sort+1", true);
        List<TempletModuleDTO> list = templetModuleService.selectList(wrapperA);
        return new Result<>(list);
    }

    /**
     * 获取平台首页 所有功能
     *
     * @return
     */
    @ApiOperation(value = "平台金融服务 所有模块", notes = "平台金融服务 所有模块")
    @PostMapping(value = "/queryFinancialTempletModuleList")
    public Result<List<TempletModuleDTO>> queryFinancialTempletModuleList(Long templetId) {

        if (templetId == null || templetId <= 0) {
            Wrapper wrapper = new EntityWrapper();
            wrapper.eq("shop_id", 0);
            wrapper.eq("type", 3);
            wrapper.eq("is_default", 0);
            wrapper.ne("parent_template_id", 0);
            TempletPlatformRelationDTO relationDTO = templetPlatformRelationService.selectOne(wrapper);
            if (relationDTO != null) {
                templetId = relationDTO.getTempletId();
            }
        }

        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("templet_id", templetId);
        wrapper.eq("is_publish", 1);
        wrapper.eq("status", 0);
        wrapper.orderBy("sort", true);
        List<TempletModuleDTO> list = templetModuleService.selectList(wrapper);
        return new Result<>(list);
    }

    /**
     * 获取平台活动页 所有功能
     *
     * @return
     */
    @ApiOperation(value = "平台活动页 所有模块", notes = "平台活动页 所有模块")
    @PostMapping(value = "/getListPlatformActiveTemplateModule")
    public Result<List<TempletModuleDTO>> getListPlatformActiveTemplateModule() {
        List<TempletModuleDTO> templetModuleDTOList = iTempletInfoService.getListPlatformActiveTemInfoModule();
        TemplateModuleSortVO templateModuleSortVO = new TemplateModuleSortVO();
        templateModuleSortVO.setTempletModuleDTOList(templetModuleDTOList);//添加功能
        return new Result<>(templetModuleDTOList);
    }

    /**
     * 店铺首页 所有功能 根据店铺id
     *
     * @return
     */
    @SysLogs(value = "访问店铺首页")
    @ApiOperation(value = "店铺首页 所有功能 根据店铺id", notes = "店铺首页 所有功能 根据店铺id")
    @PostMapping(value = "/getListDefaultShopTemplateModuleByShopId")
    public Result<List<TempletModuleDTO>> getListDefaultShopTemplateModuleByShopId(Long shopId) {
        List<TempletModuleDTO> templetModuleDTOList = iTempletInfoService.getDefaultListShopTemInfoModule(shopId);
        Result<List<TempletModuleDTO>> result = new Result<>(templetModuleDTOList);
        //冻结状态不展示
        boolean b = shopInfoService.checkShopIsClose(shopId);
        if (!b) {
            result.setCode(CodeEnum.SHOP_HOME_FREEZE.getCode());
        }
        return result;
    }

    /**
     * 店铺活动页 所有功能 根据店铺id
     *
     * @return
     */
    @ApiOperation(value = "店铺活动页 所有功能 根据店铺id", notes = "店铺活动页 所有功能 根据店铺id")
    @PostMapping(value = "/getListDefaultShopActiveTemplateModuleByShopId")
    public Result<List<TempletModuleDTO>> getListDefaultShopActiveTemplateModuleByShopId(Long shopId) {
        //冻结状态不展示
        boolean b = shopInfoService.checkShopIsClose(shopId);
        if (!b) {
            return new Result<>(CodeEnum.SHOP_HOME_FREEZE);
        }
        List<TempletModuleDTO> templetModuleDTOList = iTempletInfoService.getDefaultListShopActiveTemInfoModule(shopId);
        return new Result<>(templetModuleDTOList);
    }

    /**
     * 获取模板id获取所有的功能(用于模板装修查看)
     *
     * @return
     */
    @ApiOperation(value = "获取模板id获取所有的功能", notes = "获取模板id获取所有的功能")
    @PostMapping(value = "/getListTemplateModuleByTemplateId")
    public Result<List<TempletModuleDTO>> getListTemplateModuleByTemplateId(Long templateId) {
        List<TempletModuleDTO> templetModuleDTOList = iTempletInfoService.getListTemplateModuleById(templateId);
        return new Result<>(templetModuleDTOList);
    }

    /**
     * 店铺装修
     *
     * @param templetModuleBOListStr
     * @return
     */
    @ApiOperation(value = "店铺装修", notes = "店铺装修")
    @PostMapping(value = "/issueShopTemplate")
    public Result issueShopTemplate(String templetModuleBOListStr) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.issueShopTemplate(templetModuleBOList);
    }

    /**
     * 店铺活动页装修
     *
     * @param templetModuleBOListStr
     * @return
     */
    @ApiOperation(value = "店铺活动页装修", notes = "店铺活动页装修")
    @PostMapping(value = "/issueShopActiveTemplate")
    public Result issueShopActiveTemplate(String templetModuleBOListStr, @RequestParam(value = "name", required = false) String name) {
        List<TempletModuleBO> templetModuleBOList = JSON.parseArray(templetModuleBOListStr, TempletModuleBO.class);
        return iTempletInfoService.issueActiveTemplate(templetModuleBOList, name);
    }

    /**
     * 获取最高销售商品
     *
     * @return
     */
    @ApiOperation(value = "获取最高销售商品", notes = "获取最高销售商品")
    @PostMapping(value = "/getHighShop")
    public Result<List<CommodityPriceVO>> getHighShop(OrderCommodityHighBO orderCommodityHighBO) {
        List<CommodityPriceVO> highShop = orderCommodityRelationService.getViewsGoods();
        return new Result<>(highShop);
    }

    /**
     * 获取平台 模板
     *
     * @return
     */
    @ApiOperation(value = "平台 模板", notes = "平台 模板")
    @PostMapping(value = "/getPlatformTemplateInfo")
    public Result getPlatformTemplateInfo() {
        TempletInfoVO templetInfoVO = iTempletInfoService.getPlatformTemInfo();
        return new Result<>(templetInfoVO);
    }


    /**
     * 平台活动页 模板
     *
     * @return
     */
    @ApiOperation(value = "平台活动页 模板", notes = "平台活动页 模板")
    @PostMapping(value = "/getPlatformActiveTemplateInfo")
    public Result getPlatformActiveTemplateInfo() {
        TempletInfoVO templetInfoVO = iTempletInfoService.getPlatformActiveTemInfo();
        return new Result<>(templetInfoVO);
    }

    /**
     * 获取店铺 首页
     *
     * @param shopId
     * @return
     */
    @PostMapping("/getShopTemplateInfoByShopId")
    @ApiOperation(value = "店铺 首页", notes = "店铺 首页")
    public Result getShopTemplateInfoByShopId(Long shopId) {
        if (null == shopId)
            return new Result<>(CodeEnum.FAIL_100002);

        TempletInfoVO templetInfoVO = iTempletInfoService.getDefaultShopTemListByShopId(shopId);
        return new Result<>(templetInfoVO);
    }

    /**
     * 获取店铺活动页 模板
     *
     * @return
     */
    @ApiOperation(value = "店铺活动页 模板", notes = "店铺活动页 模板")
    @PostMapping(value = "/getShopActiveTemplateInfo")
    public Result getShopActiveTemplateInfo(Long shopId) {
        TempletInfoVO templetInfoVO = iTempletInfoService.getDefaultShopActiveTemListByShopId(shopId);
        return new Result<>(templetInfoVO);
    }

    /**
     * 店铺 模板 根据模板id和店铺id
     *
     * @param shopId
     * @return
     */
    @PostMapping("/getDShopTemplateInfoByShopId")
    @ApiOperation(value = "店铺 模板", notes = "店铺 模板")
    public Result getDShopTemplateInfoByShopId(Long shopId, Long temId) {
        if (null == shopId) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        if (null == temId) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        TempletInfoVO templetInfoVO = iTempletInfoService.getShopDecorationInfoByShopId(temId, shopId);
        return new Result<>(templetInfoVO);
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<TempletInfoDTO> get(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        TempletInfoDTO templetInfoDTO = iTempletInfoService.selectById(id);
        return new Result<>(templetInfoDTO);
    }

    @ApiOperation(value = "模版表列表", notes = "模版表列表")
    @PostMapping("/queryPageList")
    public Result<Page<TempletInfoDTO>> queryPageList(TempletInfoBO templetInfoBO) {
        Wrapper<TempletInfoDTO> wrapper = new EntityWrapper<>();
        Page<TempletInfoDTO> page = new Page<>(templetInfoBO.getPageNo(), templetInfoBO.getPageSize());
        page = iTempletInfoService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @PostMapping("/getHomepageUserOrderStatusStatistics")
    @ApiOperation(value = "商城首页用户订单状态统计", notes = "商城首页用户订单状态统计")
    public Result getHomepageUserOrderStatusStatistics(Long shopId) {
        if (shopId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(iTempletInfoService.getHomepageUserOrderStatusStatistics(shopId));
    }
}

